package com.game.backend.service.impl.user.bot;

import com.game.backend.mapper.BotMapper;
import com.game.backend.model.Bot;
import com.game.backend.model.User;
import com.game.backend.service.impl.utils.UserDetailsImpl;
import com.game.backend.service.user.bot.RemoveService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class RemoveServiceImpl implements RemoveService {
    @Autowired
    private BotMapper botMapper;

    @Override
    public Map<String, String> remove(int id) {
        UsernamePasswordAuthenticationToken authentication = (UsernamePasswordAuthenticationToken)
                SecurityContextHolder.getContext().getAuthentication();
        UserDetailsImpl loginUser = (UserDetailsImpl) authentication.getPrincipal();

        Map<String,String> map = new HashMap<>();

        User user = loginUser.getUser();
        Bot bot = botMapper.selectById(id);

        if(bot == null){
            map.put("msg","bot不存在");
            return map;
        }

        if(!user.getId().equals(bot.getUserId())){
            map.put("msg","您无权限删除该bot");
            return map;
        }

        int res = botMapper.deleteById(id);
        if(res == 1){
            map.put("msg","success");
        }else{
            map.put("msg","删除bot失败");
        }
        return map;
    }
}
